//
//  AppDelegate.m
//  3DTouch
//
//  Created by yao zhaofeng on 2018/4/16.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import "AppDelegate.h"
#import "DataUtil.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    if (@available(iOS 9.0, *)) {
//        UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
//        //如果是从快捷选项标签启动app，则根据不同标识执行不同操作，然后返回NO，防止调用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
//        if (shortcutItem) {
//            //判断先前我们设置的快捷选项标签唯一标识，根据不同标识执行不同操作
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:shortcutItem.type message:@"didFinishLaunchingWithOptions" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//            [alertView show];
//            return NO;
//        }
//    } else {
//        // Fallback on earlier versions
//    }
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
API_AVAILABLE(ios(9.0)){
    if ([shortcutItem.type isEqualToString:@"com.xy.search"])
    {
        // do something ...
    }
    else if ([shortcutItem.type isEqualToString:@"com.xy.play"])
    {
        // do something ...
    }
    // ...
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:shortcutItem.type message:@"show by 3d touch" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    
    if (completionHandler) {
        completionHandler(YES);
    }
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options API_AVAILABLE(ios(9.0)){
    if ([options[UIApplicationOpenURLOptionsSourceApplicationKey]  isEqual: @"com.xy.3DTouch.TodayWidget"]) {
        NSString *host = [url host];
        NSLog(@"host is %@", host);
        if ([host isEqualToString:@"alert"]) {
            NSString *title = [DataUtil readDataByNSFileManagerForPath:@""];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:@"show by TodayWidget" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        } else if ([host isEqualToString:@"hello"]) {
            NSString *title = [DataUtil readDataFromNSUserDefaultsForKey:@"Widget"];
            NSURLComponents *urlComponent = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
            NSArray *queryItems = urlComponent.queryItems;
            NSString *msg = [NSString stringWithFormat:@"gameid=%@ productid=%@", [DataUtil valueForKey:@"gameid" fromQueryItems:queryItems], [DataUtil valueForKey:@"productid" fromQueryItems:queryItems]];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
    }
    return YES;
}

@end
